<?php
/**
 * Created by PhpStorm.
 * User: xi
 * Date: 2020-05-02
 * Time: 16:54
 */

namespace app\api\service;


use app\admin\model\Air;
use app\admin\model\Roomclean;
use app\admin\model\Shopdevice;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproombook;
use app\admin\model\Shopstaff;
use app\admin\model\Shoptable;
use app\admin\model\Shoptableorder;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\admin\model\Userwithdraw;

class AirService
{
    public $openCmdUrl = "http://47.97.67.56:8080/cmd/open";

    //public $airCmd = "http://47.97.67.56:8881/qx/air";
    public $airCmd = "http://112.74.78.56:8881/qx/air";


    public function closeAir($shop_id, $on)
    {

        $airModel = new \app\admin\model\device\Air();
        $list = $airModel->where('shop_id', $shop_id)->group('device_sn')->select();
        foreach ($list as $key => $val) {
            $req = array();
            $req['deviceSn'] = $val['device_sn'];
            $req['on'] = $on;
            $req['mode'] = $val['mode'];
            $req['temp'] = $val['temp'];

            error_log("===air===" . json_encode($req));

            $ret = http_post_json($this->airCmd, json_encode($req));
            $ret = json_decode($ret, true);
            if ($ret['code'] == 0) {
                $msg = "正在打开中";
            }
        }


    }


    public function banAir($table_id, $on)
    {

        $airModel = new \app\admin\model\device\Air();
        $air = $airModel->where('table_id', $table_id)->find();
        if ($air && $air['is_open'] == 1) {
            $req = array();
            $req['deviceSn'] = $air['device_sn'];
            $req['on'] = $on;
            $req['mode'] = $air['mode'];
            $req['temp'] = $air['temp'];
            error_log("==banAir=air===" . json_encode($req));

            $ret = http_post_json($this->airCmd, json_encode($req));
            $ret = json_decode($ret, true);
            if ($ret['code'] == 0) {
                $msg = "正在打开中";
            }
        }
    }

    public function airCheckV2($shop_id, $table_id)
    {

        $shopTableModel = new Shoptable();
        $airModel = new \app\admin\model\device\Air();
        $air = $airModel->where('table_id', $table_id)->find();

        if ($air && $air['is_open'] == 1) {

            $tableOrderModel = new Shoptableorder();
            $tableOrder = $tableOrderModel->where('table_id', $table_id)->where('status', 'doing')->find();

            $isFree = 1;
            $airList = $airModel->where('device_sn', $air['device_sn'])->select();
            $ids = array();
            foreach ($airList as $key => $val) {
                $ids[] = $val['table_id'];
            }
            $list = $shopTableModel->where('id', 'in', $ids)->select();
            foreach ($list as $key => $val) {

                if ($val['status'] == 'busy') {
                    $isFree = -1;
                }
            }
            if ($isFree == 1) {
                //close;
                $isOn = 0;
            } else {
                //open
                $isOn = 1;
            }
            if ($tableOrder) {
                $tableOrderModel->where('id', $tableOrder['id'])->update(['air_close' => $isOn]);
            }

            $req = array();
            $req['deviceSn'] = $air['device_sn'];
            $req['on'] = $isOn;
            $req['mode'] = $air['mode'];
            $req['temp'] = $air['temp'];

            error_log("===air===" . json_encode($req));

            $ret = http_post_json($this->airCmd, json_encode($req));
            $ret = json_decode($ret, true);
            if ($ret['code'] == 0) {
                $msg = "正在打开中";
            }
        }

    }

    public function airCheck($shop_id)
    {
        $shopTableModel = new Shoptable();
        $shopDeviceModel = new Shopdevice();
        if ($shop_id == 466) {
            $device = $shopDeviceModel->where('shop_id', $shop_id)->where('type', 'air')->find();
            if ($device) {
                $isFree = 1;
                $list = $shopTableModel->where('shop_id', $shop_id)->select();

                foreach ($list as $key => $val) {

                    if ($val['status'] == 'busy') {
                        $isFree = -1;
                    }
                }
                if ($isFree == 1) {
                    $sec = 0;
                } else {
                    $sec = 3600 * 3;
                }
                $sn = $device['sn'];
                $req = array();
                $req['sec'] = $sec;
                $req['orderNo'] = getOrderSn();
                $req['sn'] = $sn;
                $req['voice'] = "";

                error_log("===air req==" . json_encode($req));
                $ret = http_post_json($this->openCmdUrl, json_encode($req));
                $ret = json_decode($ret, true);
                if ($ret['code'] == 0) {
                    $msg = "正在打开中";
                }

            }
        }
    }

    public function airCheck3($sn, $sec)
    {
        $req = array();
        $req['sec'] = $sec;
        $req['orderNo'] = getOrderSn();
        $req['sn'] = $sn;
        $req['voice'] = "";
        error_log("===air airCheck3==" . json_encode($req));

        $ret = http_post_json($this->openCmdUrl, json_encode($req));
        $ret = json_decode($ret, true);
        if ($ret['code'] == 0) {
            $msg = "正在打开中";
        }
    }


    public function airCheck2($shop_id, $sec)
    {
        $shopTableModel = new Shoptable();
        $shopDeviceModel = new Shopdevice();
        if ($shop_id == 466) {
            $device = $shopDeviceModel->where('shop_id', $shop_id)->where('type', 'air')->find();


            $isFree = 1;
            $list = $shopTableModel->where('shop_id', $shop_id)->select();
            foreach ($list as $key => $val) {
                if ($val['status'] == 'busy') {
                    $isFree = -1;
                }
            }

            if ($device && $isFree != -1) {

                $sn = $device['sn'];
                $req = array();
                $req['sec'] = $sec;
                $req['orderNo'] = getOrderSn();
                $req['sn'] = $sn;
                $req['voice'] = "";
                error_log("===air airCheck2==" . json_encode($req));

                $ret = http_post_json($this->openCmdUrl, json_encode($req));
                $ret = json_decode($ret, true);
                if ($ret['code'] == 0) {
                    $msg = "正在打开中";
                }

            }
        }
    }
}